/*
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2014
 *
 * fileName:  Randoms.java
 * author:  lei
 * Company:  Cloudland Studio
 * Createdate:  2013-9-14 上午9:58:23
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2013-9-14          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.util;

import java.util.Random;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  Randoms
 * @author  lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class Randoms {
	
	private static Random RANDOM = new Random();
	
	/**
	 * <pre>
	 * 随即生成无规则的英文字符串。
	 * </pre>
	 *
	 * @param length  生成字符串的长度
	 * @return  生成好的字符串
	 */
	public static String buildString(int length) {
		int begin = 32, end = 132;
		int scope  = end - begin;
		
		StringBuffer buffer = new StringBuffer();
		char ch;
		while (length != 0) {
			ch = (char) (RANDOM.nextInt(scope) + begin);
			if (!Character.isLetterOrDigit(ch)) {
				continue;
			}
			buffer.append(ch);
			length--;
		}
		
		return buffer.toString();
	}
	
	/**
	 * <pre>
	 * 生成随机数。
	 * </pre>
	 *
	 * @param min  最小数
	 * @param max  最大数
	 * @return   
	 */
	public static int NextInt(final int min, final int max)
	{
	    int tmp = Math.abs(RANDOM.nextInt());
	    return tmp % (max - min + 1) + min;
	}

}
